package com.fractalist.man.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fractalist.base.cache.Cache;
import com.fractalist.man.dao.IFilterDao;
import com.fractalist.man.service.IFilterService;

@Service
public class FilterService implements IFilterService {
	
	@Autowired
	private IFilterDao filterDao;

	@Override
	public Long add(String sql) {
		return filterDao.add(sql);
	}

	@Override
	public Boolean update(String sql) {
		return filterDao.update(sql);
	}

	@Override
	public Map<String, Object> queryOneRow(String sql) {
		return filterDao.queryOneRow(sql);
	}

	@Override
	public List<Map<String, Object>> queryList(String sql) {
		return filterDao.queryList(sql);
	}

	@Override
	public int[] batchUpdate(String[] sql) {
		return filterDao.batchUpdate(sql);
	}

	@Override
	public void excute(String sql) {
		filterDao.excute(sql);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> queryOneRowFromDbOrCache(String sql,
			String expire) {
		Map<String, Object> result = (Map<String, Object>) Cache.get(sql);
		if (result == null) {
			result = queryOneRow(sql);
			Cache.add(sql, result, expire);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> queryListFromDbOrCache(String sql,
			String expire) {
		List<Map<String, Object>> result = (List<Map<String, Object>>) Cache
				.get(sql);
		if (result == null) {
			result = queryList(sql);
			Cache.add(sql, result, expire);
		}
		return result;
	}
}
